/**
 * Created with JetBrains WebStorm.
 * User: zhaoyanchao
 * Date: 13-7-6
 * Time: 下午3:21
 * To change this template use File | Settings | File Templates.
 */
var http = require('http'),
    mysql = require('mysql'),
    redis = require('redis'),
    weather = require('./getWeather'),
    getcityCode = require('./getcityCode').getcityCode,
    config = require('./config').config;


function show(data){
    var l = data.length;
    for(var i=0;i<l;i++){
        (function(i){
            var cityCode=data[i]['cityCode'];
            var options = {
                hostname: 'm.weather.com.cn',
                port: 80,
                path: '/data/'+cityCode+'.html',
                method: 'GET'
            };
            weather.getWeatherBuffers(options,function(chunk){

                    var str = JSON.parse(chunk);
                    var weatherInfo =  str.weatherinfo;
                    var nowtime=Math.round(new Date().getTime()/1000);
                        var cityName = weatherInfo.city,
                            cityCode = weatherInfo.cityid,
                            date = weatherInfo.date_y,
                            week = weatherInfo.week,
                            releaseTime = weatherInfo.fchh,
                            temperature = weatherInfo.temp2,
                            weather = weatherInfo.weather2,
                            wind = weatherInfo.wind2,
                            windScale = weatherInfo.fx2,
                            dressIndex =weatherInfo.index,
                            dressSuggestion = weatherInfo.index_d,
                            ultravioletIndex = weatherInfo.index_uv,
                            washCarIndex = weatherInfo.index_xc,
                            travelIndex = weatherInfo.index_tr,
                            comfortIndex = weatherInfo.index_co,
                            st = weatherInfo.st2,
                            morningExerciseIndex = weatherInfo.index_cl,
                            dryingIndex = weatherInfo.index_ls,
                            allergyIndex = weatherInfo.index_ag;

                        var connection = mysql.createConnection(config.daokeMap);
                        connection.connect();
                        //id, time, cityName, cityCode, date, week, fchh, temperature, weather, wind, windScale, dressIndex, dressSuggestion, ultravioletIndex, washCarIndex, travelIndex, comfortIndex, st, morningExerciseIndex, dryingIndex, allergyIndex, text
                        var sql ="INSERT INTO daokeMap.weatherInfo VALUES ('"+"',from_unixtime("+nowtime+"),'"+cityName+"','"+cityCode+"','"+date+"','"+week+"','"+releaseTime+"','"+temperature+"','"+weather+"','"+wind+"','"+windScale+"','"+dressIndex+"','"+dressSuggestion+"','"+ultravioletIndex+"','"+washCarIndex+"','"+travelIndex+"','"+comfortIndex+"','"+st+"','"+morningExerciseIndex+"','"+dryingIndex+"','"+allergyIndex+"','"+chunk+"')";
                        connection.query(sql,function(err,reply){
                            if (err) throw err;
                            if(reply['affectedRows'] ==1)
                                console.log(cityName+' weatherInfo OK!')
                            connection.end();
                        })


            });
        })(i)
    }
}
getcityCode(show);
